/*
 * CatalogHelper.java
 *
 * Created on July 7, 2007, 11:17 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package helpers;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import model.Product;

/**
 *
 * @author Alirio
 */
public class CatalogHelper {
  
  /** Creates a new instance of CatalogHelper */
  public CatalogHelper() {
  }
  
  public synchronized static void createCatalog(ServletContext sc) {
    Statement stmt;
    Connection cnn = (Connection) sc.getAttribute("cnn");
    try {
        stmt = cnn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from products where estado='1' order by brand");
        List productCatalog = new ArrayList();
        while(rs.next()) {
          Product p = new Product();
          p.setId(rs.getString("product_id"));
          p.setDescription(rs.getString("description"));
          p.setPrice1(rs.getDouble("price1"));
          p.setPrice2(rs.getDouble("price2"));
          p.setPrice3(rs.getDouble("price3"));
          p.setPrice4(rs.getDouble("price4"));
          p.setPrice5(rs.getDouble("price5"));
          p.setPrice6(rs.getDouble("price6"));
          p.setPrice7(rs.getDouble("price7"));
          p.setPrice8(rs.getDouble("price8"));
          p.setPrice9(rs.getDouble("price9"));
          p.setPrice10(rs.getDouble("price10"));
          p.setPrimaryUnit(rs.getString("primary_unit"));
          p.setAlternativeUnit(rs.getString("alternative_unit"));
          p.setStock(rs.getDouble("stock"));
          p.setConversionRate(rs.getFloat("conversion_rate"));
          p.setWarehouse(rs.getString("warehouse"));
          p.setBrand(rs.getString("brand"));
          p.setTarget(rs.getString("target"));
          productCatalog.add(p);
        }
        sc.setAttribute("PRODUCT_CATALOG_MASTER", productCatalog);
    } catch (SQLException e) {
      System.out.println("Error creando el catalogo master de productos." + e.getMessage());
    }    
  }
  
    public synchronized static void createCatalog(ServletContext sc,String warehouse) {
    Statement stmt;
    Connection cnn = (Connection) sc.getAttribute("cnn");
    try {
        stmt = cnn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from products where warehouse in ("+ warehouse +") and estado='1' order by brand");
        List productCatalog = new ArrayList();
        while(rs.next()) {
          Product p = new Product();
          p.setId(rs.getString("product_id"));
          p.setDescription(rs.getString("description"));
          p.setPrice1(rs.getDouble("price1"));
          p.setPrice2(rs.getDouble("price2"));
          p.setPrice3(rs.getDouble("price3"));
          p.setPrice4(rs.getDouble("price4"));
          p.setPrice5(rs.getDouble("price5"));
          p.setPrice6(rs.getDouble("price6"));
          p.setPrice7(rs.getDouble("price7"));
          p.setPrice8(rs.getDouble("price8"));
          p.setPrice9(rs.getDouble("price9"));
          p.setPrice10(rs.getDouble("price10"));
          p.setPrimaryUnit(rs.getString("primary_unit"));
          p.setAlternativeUnit(rs.getString("alternative_unit"));
          p.setStock(rs.getDouble("stock"));
          p.setConversionRate(rs.getFloat("conversion_rate"));
          p.setWarehouse(rs.getString("warehouse"));
          p.setBrand(rs.getString("brand"));
          p.setTarget(rs.getString("target"));
          productCatalog.add(p);
        }
        sc.setAttribute("PRODUCT_CATALOG_MASTER", productCatalog);
    } catch (SQLException e) {
      System.out.println("Error creando el catalogo master de productos." + e.getMessage());
    }    
  }
  
  
  public static Product getProduct(String id, ServletContext sc) {
    Product p = new Product(id,"");
    ArrayList masterCatalog = (ArrayList) sc.getAttribute("PRODUCT_CATALOG_MASTER");
    Iterator iterator = masterCatalog.iterator();
    if (masterCatalog.contains(p)) {
      return (Product) masterCatalog.get(masterCatalog.indexOf(p));
    } else {
      return null;
    }
  } 
}
